package com.xyz.study.spring.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringValueResolver;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;

/**
 * 演示bean生命周期
 *
 * @author xyz
 * @version 1.0.0
 * @since 2021/7/23
 */
public class SpringTestBean implements
        BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ApplicationEventPublisherAware, ResourceLoaderAware,
        EmbeddedValueResolverAware, EnvironmentAware,MessageSourceAware,ApplicationContextAware, ServletContextAware,
        InitializingBean, DisposableBean, SmartInitializingSingleton {


    /**
     * 该方法用于获取在容器中的Name
     * @param name
     */
    @Override
    public void setBeanName(String name) {
        System.out.println("调用BeanNameAware.setBeanName(String name)");
    }
    /**
     * 该方法用于获取自身在容器中的ClassLoader
     * @param classLoader
     */
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("调用BeanClassLoaderAware.setBeanClassLoader(ClassLoader classLoader)");
    }
    /**
     * 该方法用于获取自身在容器中的BeanFactory
     * @param beanFactory
     */
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用BeanFactoryAware.setBeanFactory(BeanFactory beanFactory)");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("调用ApplicationContextAware.setApplicationContext(ApplicationContext applicationContext)");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用InitializingBean.afterPropertiesSet()");
    }

    public void initMethod(){
        System.out.println("调用Bean的init-method");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("调用DisposableBean.destroy()");
    }

    public void destroyMethod(){
        System.out.println("调用Bean的destroy-method");
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println("调用ApplicationEventPublisherAware.setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)");
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        System.out.println("调用EmbeddedValueResolverAware.setEmbeddedValueResolver(StringValueResolver resolver)");
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("调用EnvironmentAware.setEnvironment(Environment environment)");
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println("调用MessageSourceAware.setMessageSource(MessageSource messageSource)");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("调用ResourceLoaderAware.setResourceLoader(ResourceLoader resourceLoader)");
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        System.out.println("调用ServletContextAware.setServletContext(ServletContext servletContext)");
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("调用Bean的@PostConstruct");
    }

    @PreDestroy
    public void preDestroy(){
        System.out.println("调用Bean的@PreDestroy");
    }

    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("调用SmartInitializingSingleton.afterSingletonsInstantiated");
    }
}
